//
//  ZYLFastlookAllContentViewController.m
//  Cartoon
//
//  Created by dllo on 15/7/14.
//  Copyright (c) 2015年 dllo. All rights reserved.
//
#import "BaseHeader.h"
#import "ZYLFastlookAllContentViewController.h"
#import "LSYFastLookExcelTableViewCell.h"
#import "AFNetworkHandler.h"
#import "LSYFastLookAllConentModel.h"


#import "ZYLFastLookIntroduceView.h"
@interface ZYLFastlookAllContentViewController ()<UITableViewDelegate, UITableViewDataSource, buttonDelegate>
@property (nonatomic, retain) UITableView *contentTableView;
@property (nonatomic, retain) ZYLFastLookIntroduceView *introduceView ;
@property (nonatomic, retain) UIView *placeview;
@property (nonatomic, retain) NSMutableArray *topicDataArr;
@property (nonatomic, retain) NSDictionary *introduceDataDic;
@property (nonatomic, retain) UIScrollView *scroV;
@property (nonatomic, retain) NSString *queueCount;
@end

@implementation ZYLFastlookAllContentViewController

- (void)dealloc
{
    [_queueCount release];
    [_scroV release];
    [_introduceDataDic release];
    [_topicDataArr release];
    [_placeview release];
    [_introduceView release];
    [_contentTableView release];
    [super dealloc];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.queueCount = @"0";
    [self getData];
}

#pragma mark - 页面布局 封装在 ZYLFastLookIntroduceView 类中

- (void)createIntroduce
{
    self.introduceView = [ZYLFastLookIntroduceView introduceWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 350 * SCREEN_HEIGHT / 667) dataDic:self.introduceDataDic];
    self.introduceView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.introduceView];
    self.introduceView.lsyDelegate = self;
}

#pragma mark - 显示章节列表
- (void)createTableView
{
    self.contentTableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, self.introduceView.frame.size.height, SCREEN_WIDTH, SCREEN_HEIGHT - self.introduceView.frame.size.height - 44) style:UITableViewStylePlain] autorelease];
    self.contentTableView.delegate = self;
    self.contentTableView.dataSource = self;
    [self.view addSubview:self.contentTableView];
    self.contentTableView.bounces = NO;
}

#pragma mark - tableView 协议方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSYFastLookExcelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"l"];
    if (!cell) {
        cell = [[[LSYFastLookExcelTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"l"] autorelease];
    }
    self.contentTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 15);
    self.contentTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    cell.topicModel = self.topicDataArr[indexPath.row];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.topicDataArr.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 70;
}

/**
 *  返回按钮的协议方法
 */
- (void)backButton
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)queueButton:(NSString *)Action
{
    
    if ([@"0" isEqualToString:Action]) {
        self.queueCount = Action;
        [self getDataTableView];
    } else if ([@"1" isEqualToString:Action]){
        self.queueCount = Action;
        [self getDataTableView];
    }
}



#pragma mark - 数据处理
- (void)getData
{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        
        NSString *strTemp = [@"http://api.kuaikanmanhua.com/v1/topics/" stringByAppendingString:self.ID.stringValue];
        NSString *str2 = [strTemp stringByAppendingString:@"?sort="];
        NSString *str = [str2 stringByAppendingString:self.queueCount];
        [AFNetworkHandler GETwithAFNByURL:str completion:^(id result) {
            self.introduceDataDic = [result objectForKey:@"data"];
            NSMutableArray *topicArr = [self.introduceDataDic objectForKey:@"comics"];
            self.topicDataArr = [LSYFastLookAllConentModel arrayWithModelByArray:topicArr];
            [self createIntroduce];
            [self createTableView];
        }];
    });
}

- (void)getDataTableView
{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        
        NSString *strTemp = [@"http://api.kuaikanmanhua.com/v1/topics/" stringByAppendingString:self.ID.stringValue];
        NSString *str2 = [strTemp stringByAppendingString:@"?sort="];
        NSString *str = [str2 stringByAppendingString:self.queueCount];
        [AFNetworkHandler GETwithAFNByURL:str completion:^(id result) {
            self.introduceDataDic = [result objectForKey:@"data"];
            NSMutableArray *topicArr = [self.introduceDataDic objectForKey:@"comics"];
            self.topicDataArr = [LSYFastLookAllConentModel arrayWithModelByArray:topicArr];
            [self.contentTableView reloadData];
            
        }];
    });
}






- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
